package com.ohc.itnanls.数组;

/**
 * 斐波那契数列 +剪枝
 */
public class fibonacci1 {

    public static int fibonacci2(int n) {
        int[] arr = new int[n];
        arr[0] = 1;
        arr[1] = 1;
        for (int i = 2; i <= n - 1; i++) {
            arr[i] = arr[i - 1] + arr[i - 2];
        }
        return arr[n - 1];
    }


    public static int fibonacci3(int n, int[] num) {
        if (num[n - 1] > 0) {
            return num[n - 1];
        }
        if (n == 1 || n == 2) {
            return num[n - 1] = 1;
        } else {
            num[n - 1] = fibonacci3(n - 1, num) + fibonacci3(n - 2, num);
        }
        return num[n - 1];
    }

    public static void main(String[] args) {
        int i = fibonacci2(6);
        int i1 = fibonacci3(6, new int[6]);
        System.out.println("i = " + i);
        System.out.println("i1 = " + i1);
    }
}
